package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import dto.BorrowBookDTO;
import dto.LogonDTO;
import dto.ReturnBookDTO;

public class BorrowReturnBookPage extends JDialog {

	private static final long serialVersionUID = 1L;

	private final JPanel contentPanel = new JPanel();

	private JLabel borrowLabelMessage;
	private JLabel returnLabelMessage;
	private JTextField textFieldBorrowerId;
	private JTextField textFieldReturnerId;
	private JTextField textFieldCatalog;
	private JTextField textFieldUsername;
	private JTextField textFieldLevel;

	private JButton btnBorrowABook = new JButton("Borrow book");
	private JButton btnReturnABook = new JButton("Return book");

	private LogonDTO logonDTO;
	private BorrowBookDTO borrowBookDTO;
	private ReturnBookDTO returnBookDTO;

	public BorrowBookDTO getBorrowBookDTO() {
		borrowBookDTO.setBorrowerId(textFieldBorrowerId.getText().trim());

		return borrowBookDTO;
	}

	public ReturnBookDTO getReturnBookDTO() {
		returnBookDTO.setReturnerId(textFieldReturnerId.getText().trim());
		returnBookDTO.setDeweyDecimalNo(textFieldCatalog.getText().trim());

		return returnBookDTO;
	}

	public LogonDTO getLogonDTO() {
		return logonDTO;
	}

	/**
	 * Create the dialog.
	 */
	public BorrowReturnBookPage(LogonDTO logonDTO) {

		this.logonDTO = logonDTO;

		borrowBookDTO = new BorrowBookDTO();
		returnBookDTO = new ReturnBookDTO();

		setBounds(100, 100, 571, 468);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel
				.setLayout(new MigLayout(
						"",
						"[15px][142.00px][][187.00px,grow][40][101px][72.00px][]",
						"[19px][][][][14px][23px][14px][][][][14px][][][][][][][8.00][][][][][26.00px]"));
		{
			JLabel lblJaLibrary = new JLabel("J&A Library");
			lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
			lblJaLibrary.setForeground(Color.BLUE);
			contentPanel.add(lblJaLibrary, "cell 3 0,alignx center,aligny top");
		}
		{
			JLabel lblBorrowreturnBook = new JLabel("Borrow/Return Book");
			contentPanel.add(lblBorrowreturnBook,
					"cell 3 1,alignx center,aligny top");
		}
		{
			JLabel lblBorrowABook = new JLabel("Borrow a book");
			lblBorrowABook.setFont(new Font("Tahoma", Font.BOLD, 14));
			contentPanel.add(lblBorrowABook, "cell 1 3");
		}
		{
			JLabel lblUsername = new JLabel("Username:");
			contentPanel
					.add(lblUsername, "cell 1 5,alignx right,aligny center");
		}

		textFieldBorrowerId = new JTextField();
		textFieldBorrowerId.setDocument(new JTextFieldLimit(20));
		contentPanel.add(textFieldBorrowerId, "cell 3 5,growx,aligny center");
		textFieldBorrowerId.setColumns(10);

		// BORROW BOOK button
		contentPanel.add(btnBorrowABook, "cell 5 5,alignx left,aligny top");

		{
			JLabel lblusernameIsRequired = new JLabel(
					"*input borrower's username ");
			lblusernameIsRequired.setFont(new Font("Tahoma", Font.PLAIN, 10));
			contentPanel.add(lblusernameIsRequired,
					"cell 1 7 3 1,alignx left,aligny top");
		}
		{
			borrowLabelMessage = new JLabel("     ");
			borrowLabelMessage.setForeground(Color.BLUE);
			contentPanel.add(borrowLabelMessage, "cell 0 9 5 1");

		}
		{
			JLabel label = new JLabel("     ");
			contentPanel.add(label, "cell 0 10,alignx left,aligny top");
		}
		{
			JSeparator separator = new JSeparator();
			contentPanel.add(separator, "cell 1 10 7 1,growx,gapy 10");
		}
		{
			JLabel lblNewLabel = new JLabel("Return a book");
			lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
			contentPanel.add(lblNewLabel, "cell 1 12 2 1");
		}
		{
			JLabel label = new JLabel("Username:");
			contentPanel.add(label, "cell 1 14,alignx right");
		}
		{
			textFieldReturnerId = new JTextField();
			textFieldReturnerId.setDocument(new JTextFieldLimit(20));
			contentPanel.add(textFieldReturnerId, "cell 3 14,growx");
			textFieldReturnerId.setColumns(10);
		}
		{
			JLabel lblCatalogddc = new JLabel("Catalog (DDC):");
			lblCatalogddc.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblCatalogddc,
					"cell 1 15,alignx right,aligny center");
		}
		{
			textFieldCatalog = new JTextField();
			textFieldCatalog.setDocument(new JTextFieldLimit(20));
			contentPanel.add(textFieldCatalog, "cell 3 15,growx,aligny center");
			textFieldCatalog.setColumns(10);
		}

		// RETURN button
		contentPanel.add(btnReturnABook, "cell 5 15,alignx left,aligny top");
		{
			JLabel lblinputCatalogNo = new JLabel(
					"*input borrower's username or the catalog no. of the book to be returned");
			lblinputCatalogNo.setFont(new Font("Tahoma", Font.PLAIN, 10));
			contentPanel.add(lblinputCatalogNo, "cell 1 18 5 1");
		}
		{
			JButton btnBack = new JButton("Back");
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			contentPanel.add(btnBack, "cell 6 21 2 1,alignx center,aligny top");
		}

		returnLabelMessage = new JLabel("     ");
		returnLabelMessage.setForeground(Color.BLUE);
		contentPanel.add(returnLabelMessage, "cell 0 20 5 1");

		{
			textFieldUsername = new JTextField();
			textFieldUsername.setEditable(false);
			textFieldUsername.setText(logonDTO.getUserName());
			contentPanel.add(textFieldUsername,
					"cell 0 22 3 1,growx,aligny top");
			textFieldUsername.setColumns(10);
		}
		{
			textFieldLevel = new JTextField();
			textFieldLevel.setEditable(false);
			textFieldLevel.setText(logonDTO.getUserTitle());
			contentPanel.add(textFieldLevel, "cell 5 22 2 1,growx,aligny top");
			textFieldLevel.setColumns(10);
		}
	}

	public void addBorrowBookButtonListener(ActionListener al) {
		btnBorrowABook.addActionListener(al);
	}

	public void addReturnBookButtonListener(ActionListener al) {
		btnReturnABook.addActionListener(al);
	}

	public void displayBorrowMessage(String message) {
		borrowLabelMessage.setText(message);
	}

	public void displayReturnMessage(String message) {
		returnLabelMessage.setText(message);
	}

}
